<?php defined('SYSPATH') or die('No direct script access.');


class Controller_Admin_Group extends Controller_Template {

	public function before()
	{
		parent::before();

		$this->checkauth('allow_view_admin');
		$this->checkauth('allow_view_group');

	}

	public function action_list()
	{
		return $this->action_edit();
	}

	public function action_add_query()
	{
		$this->checkauth('allow_update_group');
		$this->checkmethod('POST','admin/group/edit');

		$keys = 'group_name,description';
		$_data = Validate::instance()->get_post_data($keys,'group/edit');
		if (!empty($_data['error']))
			$this->failure_post($_data['error']);

		$this->model['group']->add($_data['data']);

		$this->success('group.success_add',URL::site('admin/group/edit',NULL,FALSE));
	}

	public function action_edit()
	{
		$_data = array(
			'data' => $this->model['group']->get_list(),
			'fields' => $this->model['group']->get_fields_list(),
		);

		$this->template->_data = $_data;
		$this->template->set_filename('smarty3:admin/group_edit');
	}

	public function action_edit_query($gid)
	{
		$this->checkauth('allow_update_group');
		$this->checkmethod('POST','admin/group/edit');

		$gid = intval($gid);
		$keys = 'group_name,description';
		$_data = Validate::instance()->get_post_data($keys,'group/edit');
		if (!empty($_data['error']))
			$this->failure_post($_data['error']);

		$this->model['group']->update($gid, $_data['data']);

		$this->success('group.success_edit',URL::site('admin/group/edit',NULL,FALSE));
	}

	public function action_delete_query($gid)
	{
		$this->checkauth('allow_delete_group');
		$this->checkmethod('POST','admin/group/edit');

		$gid = intval($gid);
		$keys = 'target_gid';
		$_data = Validate::instance()->get_post_data($keys,'group/delete');
		if (!empty($_data['error']))
			$this->failure_post($_data['error']);

		$this->model['group']->delete($gid, $_data['data']['target_gid']);

		$this->success('group.success_delete',URL::site('admin/group/edit',NULL,FALSE));
	}

	public function action_edit_auth_query()
	{
		$this->checkauth('allow_update_group');
		$this->checkmethod('POST','admin/group/edit');

		$keys = array_keys($this->model['group']->get_fields_list('auth_name'));
		$_data = Validate::instance()->get_post_data($keys,'group/edit_auth');
		if (!empty($_data['error']))
			$this->failure_post($_data['error']);

		$this->model['group']->update_all_auth($_data['data'], $keys);

		$this->success('group.success_edit_auth',URL::site('admin/group/edit',NULL,FALSE));
	}

	public function action_fields_list()
	{

		$fields_data = $this->model['group']->get_fields_list();

		$this->template->_fields_data = $fields_data;
		$this->template->set_filename('smarty3:admin/group_fields_list');
	}

	public function action_add_fields_query()
	{
		$this->checkauth('allow_update_group');
		$this->checkmethod('POST','admin/group/fields_list');

		$keys = 'auth_name,pid,text,type';
		$data = Validate::instance()->get_post_data($keys,'group/edit_fields');
		if (!empty($data['error'])) 
			return $this->failure_post($data['error']);

		$_data = $this->model['group']->get_fields_byname($data['data']['auth_name']);
		if (!empty($_data))
			return $this->failure('group.failure_fields_exists', FALSE, $data['data']);

		$this->model['group']->add_fields($this->user['uid'], $data['data']);
		return $this->success('success_add_fields');
	}

	public function action_edit_fields_query($id)
	{
		$this->checkauth('allow_update_group');
		$this->checkmethod('POST','admin/group/fields_list');

		$id = intval($id);
		$data = $this->model['group']->get_fields($id);
		if (empty($data))
			return $this->failure_noexist();

		$keys = 'auth_name,pid,text,type';
		$data = Validate::instance()->get_post_data($keys,'group/edit_fields');
		if (!empty($data['error'])) 
			return $this->failure_post($data['error']);

		$_data = $this->model['group']->get_fields_byname($data['data']['auth_name']);
		if (!empty($_data) && $_data['id'] != $id)
			return $this->failure('group.failure_fields_exists', FALSE, $data['data']);

		$this->model['group']->update_fields($id, $data['data']);
		return $this->success('success_edit_fields');
	}

	public function action_delete_fields_query($id)
	{
		$this->checkauth('allow_delete_group');
		//$this->checkmethod('POST','admin/group/fields_list');

		$this->model['group']->delete_fields($id, $data['data']);
		return $this->success('success_delete_fields');
	}
}